package com.traffic.util;

import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;

public class Encrypt {

    /**
     * 16进制字符数组
     */
    private final static String[] HEX_DIGITS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
            "e", "f" };
    /**
     * AES密匙
     */
    private final static byte[] keyByte = { 0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79,
            0x51, (byte) 0xCB, (byte) 0xDD, 0x55, 0x66 }; // 16字节的密钥,可以改变

    /**
     * 一位Byte到16进制字符串的转换
     * 
     * @param b
     *            byte
     * @return String
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return HEX_DIGITS[d1] + HEX_DIGITS[d2];
    }

    /**
     * Byte数组到16进制字符串的转换
     * 
     * @param b
     *            byte[]
     * @return String
     */
    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    /**
     * 16进制字符串到Byte转换
     * 
     * @param b
     *            String
     * @return byte
     */
    private static byte HexStringTobyte(String b) {
        int By = 0;
        String b1 = b.substring(0, 1);
        int b11 = -1;
        String b2 = b.substring(1);
        int b12 = -1;
        for (int i = 0; i < 16; i++) {
            if (b1.equals(HEX_DIGITS[i])) {
                b11 = i;
            }
        }
        for (int i = 0; i < 16; i++) {
            if (b2.equals(HEX_DIGITS[i])) {
                b12 = i;
            }
        }
        By = b11 * 16 + b12;
        if (By > 256) {
            By = By - 256;
        }
        return (byte) By;
    }

    /**
     * 16进制字符串到Byte数组的转换
     * 
     * @param b
     *            String
     * @return byte[]
     */
    public static byte[] HexStringTobyteArray(String b) {
        byte[] r = new byte[32];
        for (int i = 0; i < b.length() / 2; i++) {
            r[i] = HexStringTobyte(b.substring(i * 2, i * 2 + 2));
        }
        return r;
    }

    /**
     * MD5加密
     * 
     * @param OriString
     *            String
     * @return String
     * @throws Exception
     */
    public static String encodeMD5(String OriString) throws Exception {
        String resultString = null;
        resultString = new String(OriString);
        MessageDigest md = MessageDigest.getInstance("MD5");
        resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
        return resultString;
    }

    public static String encodeAES(String sSrc) throws Exception {
        return Base64Encoding.encryptBASE64(sSrc);
    }

    // 解密
    public static String decodeAES(String sSrc) throws Exception {
        return Base64Encoding.decryptBASE64(sSrc);
    }

    public static class Base64Encoding {
        private static final BASE64Decoder decoder = new BASE64Decoder();
        private static final BASE64Encoder encoder = new BASE64Encoder();

        /**
         * BASE64加密
         * 
         * @param key
         * @return
         * @throws Exception
         */
        public static String encryptBASE64(String key) throws Exception {
            if (key == null || key.length() < 1) {
                return "";
            }
            // return new String(encoder.encode(key.getBytes()));
            return new String(Base64.encodeBase64URLSafe((new String(encoder.encode(key.getBytes()))).getBytes()));
        }

        /**
         * BASE64解密
         * 
         * @param key
         * @return
         * @throws Exception
         */
        public static String decryptBASE64(String key) throws Exception {
            if (key == null || key.length() < 1) {
                return "";
            }
            return new String(decoder.decodeBuffer(new String(Base64.decodeBase64(key.getBytes()))));
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println(new String(keyByte));
            String str = encodeAES("2");
            System.out.println(str);
            System.out.println(decodeAES(str));

            String s = Base64Encoding.encryptBASE64("2");
            System.out.println(s);
            System.out.println(Base64Encoding.decryptBASE64(s));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
